package com.stand.hardcode.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.stand.hardcode.entity.StandUser;
import com.stand.hardcode.exception.UsernameTakenException;
import com.stand.hardcode.manager.UserManager;
import com.stand.hardcode.util.ParamsVerifier;
import com.stand.hardcode.util.RequestAttributes;

//registergoogle
public class RegisterGoogleServlet extends HttpServlet{

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        if (user != null) {
            resp.setContentType("text/html");
            resp.getWriter().println("Hello, you have signed in as " + user.getNickname());
            resp.getWriter().println("<br />Email : " + user.getEmail());
            StandUser u = new StandUser(user.getNickname(), user.getEmail());
            u.setDescription("Hi, I am " +  user.getNickname());
            try {
				UserManager.StoreUser(u);
				req.getSession().setAttribute(RequestAttributes.CURRENT_USER, u);
				resp.getWriter().println("<br />You have successfully registered, click <a href=\"/home\">here</a> to return to home page.");
				
			} catch (UsernameTakenException e) {
				req.getSession().setAttribute(RequestAttributes.CURRENT_USER, u);
				 resp.getWriter().println("<br />This account is already registered. Click <a href=\"/home\">here</a> to return to home page.");
			}
            
            
        } else {
            resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
        }
	}
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
	
		
		
	}
}
